package no.ntnu.item.ttm4115.termproject.teacheragent.gui;

import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.ListSelectionModel;

import com.thoughtworks.xstream.XStream;


import no.ntnu.item.ttm4115.termproject.DeleteTreasureFromGUI;
import no.ntnu.item.ttm4115.termproject.UpdateTreasureInXML;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionOption;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionQuestion;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;

/*
 * AddTreasure.java
 *
 * Created on 29. juli 2008, 09:19
 */



/**
 *
 * @author  bruker
 */
public class ModifyTreasure extends javax.swing.JPanel {
	private TeacherGUI gui;
	private ActorSM asm;
	private Treasures selectedTreasure;
	private SessionQuestion selectedQuestion;
	private int selectedQuestionPosition;
	private String oldTreasureName;
    /** Creates new form AddTreasure */
    public ModifyTreasure(TeacherGUI gui, ActorSM asm) {
        gui.setStartPanel(this);
    	this.gui = gui;
        this.asm = asm;
    	initComponents();
    	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.gui.getContentPane());
        this.gui.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        headLabel = new javax.swing.JLabel();
        treasureNameLabel = new javax.swing.JLabel();
        treasureNameTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        addedQuestions = new javax.swing.JList();
        addQuestionsLabel = new javax.swing.JLabel();
        saveTreasureButton = new javax.swing.JButton();
        newQuestionLabel = new javax.swing.JLabel();
        newQuestionNameLabel = new javax.swing.JLabel();
        option1Label = new javax.swing.JLabel();
        option2Label = new javax.swing.JLabel();
        option3Label = new javax.swing.JLabel();
        option4Label = new javax.swing.JLabel();
        newQuestionNameTextField = new javax.swing.JTextField();
        option1TextField = new javax.swing.JTextField();
        option2TextField = new javax.swing.JTextField();
        option3TextField = new javax.swing.JTextField();
        option4TextField = new javax.swing.JTextField();
        option1CheckBox = new javax.swing.JCheckBox();
        option2CheckBox = new javax.swing.JCheckBox();
        option3CheckBox = new javax.swing.JCheckBox();
        option4CheckBox = new javax.swing.JCheckBox();
        saveQuestionButton = new javax.swing.JButton();
        removeSelectedButton = new javax.swing.JButton();
        clueLabel = new javax.swing.JLabel();
        clueTextField = new javax.swing.JTextField();
        xCoordLabel = new javax.swing.JLabel();
        xCoordTextField = new javax.swing.JTextField();
        yCoordLabel = new javax.swing.JLabel();
        yCoordTextField = new javax.swing.JTextField();
        deleteTreasureButton = new javax.swing.JButton();
        deleteQuestion = new javax.swing.JButton();
        addNewQuestion = new javax.swing.JButton();
        treasuresComboBox = new javax.swing.JComboBox();
        selectTreasureButton = new javax.swing.JButton();
        treasuresComboModel = new javax.swing.DefaultComboBoxModel();
        addedQuestionsModel = new javax.swing.DefaultListModel();

        headLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        headLabel.setText("Modify Treasure");

        treasureNameLabel.setText("Treasure Name");

        jScrollPane1.setViewportView(addedQuestions);

        addQuestionsLabel.setText("Added Questions");

        saveTreasureButton.setText("Save Treasure");
        saveTreasureButton.setEnabled(false);
        saveTreasureButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		saveTreasureButtonActionPerformed(evt);
        	}
        });

        newQuestionLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        newQuestionLabel.setText("Question");

        newQuestionNameLabel.setText("Name");

        option1Label.setText("Option1");

        option2Label.setText("Option2");

        option3Label.setText("Option3");

        option4Label.setText("Option4");
        addedQuestions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        addedQuestions.setModel(addedQuestionsModel);

        saveQuestionButton.setText("Save Question");
        saveQuestionButton.setEnabled(false);
        saveQuestionButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		saveQuestionActionPerformed(evt);
        	}
        });

        removeSelectedButton.setText("Modify Selected");
        removeSelectedButton.setEnabled(false);
        removeSelectedButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		selectQuestionActionPerformed(evt);
        	}
        });

        clueLabel.setText("Clue");

        xCoordLabel.setText("X Coord");

        yCoordLabel.setText("Y Coord");

        deleteTreasureButton.setText("Delete Treasure");
        deleteTreasureButton.setEnabled(false);
        deleteTreasureButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		deleteTreasureButtonActionPerformed(evt);
        	}
        });

        deleteQuestion.setText("Delete Question");
        deleteQuestion.setEnabled(false);
        deleteQuestion.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		deleteQuestionButtonActionPerformed(evt);
        	}
        });

        addNewQuestion.setText("Add New");
        addNewQuestion.setEnabled(false);
        addNewQuestion.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		addQuestionButtonActionPerformed(evt);
        	}
        });
        
        fillCombo(gui.getTreasures());
        treasuresComboBox.setModel(treasuresComboModel);
        
        selectTreasureButton.setText("Select");
        selectTreasureButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		selectButtonActionPerformed(evt);
        	}
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headLabel)
                .addGap(18, 18, 18)
                .addComponent(treasuresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(selectTreasureButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(238, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveTreasureButton)
                .addGap(18, 18, 18)
                .addComponent(deleteTreasureButton)
                .addContainerGap(310, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(treasureNameLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(treasureNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addQuestionsLabel)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(removeSelectedButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(yCoordLabel)
                            .addComponent(xCoordLabel)
                            .addComponent(clueTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(clueLabel)
                            .addComponent(xCoordTextField)
                            .addComponent(yCoordTextField))))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newQuestionLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newQuestionNameLabel)
                            .addComponent(option1Label)
                            .addComponent(option2Label)
                            .addComponent(option3Label)
                            .addComponent(option4Label))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(option4TextField)
                            .addComponent(option3TextField)
                            .addComponent(option2TextField)
                            .addComponent(option1TextField)
                            .addComponent(newQuestionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(option4CheckBox)
                            .addComponent(option3CheckBox)
                            .addComponent(option2CheckBox)
                            .addComponent(option1CheckBox)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(deleteQuestion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(saveQuestionButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addNewQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(headLabel)
                    .addComponent(treasuresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectTreasureButton))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(treasureNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(treasureNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addQuestionsLabel)
                            .addComponent(clueLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeSelectedButton)
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(saveTreasureButton)
                                    .addComponent(deleteTreasureButton))
                                .addGap(27, 27, 27))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(clueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(xCoordLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(xCoordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(yCoordLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(yCoordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newQuestionLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(newQuestionNameLabel)
                            .addComponent(newQuestionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option1Label)
                            .addComponent(option1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option1CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option2Label)
                            .addComponent(option2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option2CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option3Label)
                            .addComponent(option3TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option3CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option4Label)
                            .addComponent(option4TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option4CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(saveQuestionButton)
                            .addComponent(addNewQuestion))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteQuestion))))
        );
    }// </editor-fold>                       
    private void saveTreasureButtonActionPerformed(ActionEvent evt) {
		selectedTreasure.setName(treasureNameTextField.getText());
		
		Vector<String> clue = new Vector<String>();
		clue.add(clueTextField.getText());
		selectedTreasure.setClue(clue);
		selectedTreasure.setXCoordinate(Integer.parseInt(xCoordTextField.getText()));
		selectedTreasure.setYCoordinate(Integer.parseInt(yCoordTextField.getText()));
		
		for (int i = 0; i < gui.getTreasures().size(); i++) {
			if(oldTreasureName.equals(gui.getTreasures().elementAt(i).getName())){
				gui.getTreasures().elementAt(i).setName(treasureNameTextField.getText());
				gui.getTreasures().elementAt(i).setClue(clue);
				gui.getTreasures().elementAt(i).setXCoordinate(Integer.parseInt(xCoordTextField.getText()));
				gui.getTreasures().elementAt(i).setYCoordinate(Integer.parseInt(yCoordTextField.getText()));
				gui.getTreasures().elementAt(i).setQuestions(selectedTreasure.getQuestions());
			}
		}
		XStream xstream = new XStream();
		xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String treasure = xstream.toXML(selectedTreasure);
		asm.sendMessage(new UpdateTreasureInXML(treasure,oldTreasureName),new ActorAddress("/configurationAgent","ConfigurationAgent"));
	}

	private void saveQuestionActionPerformed(ActionEvent evt) {
		selectedQuestion.setCorrectAnswerIndex(getCorrectAnswerIndex());
		selectedQuestion.getOptions().elementAt(0).setAnswerOption(option1TextField.getText());
		selectedQuestion.getOptions().elementAt(1).setAnswerOption(option2TextField.getText());
		selectedQuestion.getOptions().elementAt(2).setAnswerOption(option3TextField.getText());
		selectedQuestion.getOptions().elementAt(3).setAnswerOption(option4TextField.getText());
		Vector<String> qText = new Vector<String>();
		qText.add(newQuestionNameTextField.getText());
		selectedQuestion.setQuestionText(qText);
		addedQuestionsModel.set(selectedQuestionPosition, selectedQuestion.getQuestionText().elementAt(0));
		addedQuestions.repaint();
		selectedTreasure.getQuestions().set(selectedQuestionPosition, selectedQuestion);
	}

	private void deleteTreasureButtonActionPerformed(java.awt.event.ActionEvent evt){
    	for (int i = 0; i < gui.getTreasures().size(); i++) {
			if(gui.getTreasures().elementAt(i).getName().equals(selectedTreasure.getName())){
				gui.getTreasures().remove(i);
			}
		}
		XStream xstream = new XStream();
		xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String xmlTreasure = xstream.toXML(selectedTreasure);
		asm.sendMessage(new DeleteTreasureFromGUI(xmlTreasure), new ActorAddress("/configurationAgent","ConfigurationAgent"));
		resetTreasureScheme();
		resetQuestionScheme();
    }
    private void deleteQuestionButtonActionPerformed(java.awt.event.ActionEvent evt){
    	for (int i = 0; i < gui.getTreasures().size(); i++) {
			if(gui.getTreasures().elementAt(i).getName().equals(selectedTreasure.getName())){
				for (int j = 0; j < gui.getTreasures().elementAt(i).getQuestions().size(); j++) {
					if(gui.getTreasures().elementAt(i).getQuestions().elementAt(j).getQuestionText().get(0).equals(selectedQuestion.getQuestionText().get(0))){
						gui.getTreasures().elementAt(i).getQuestions().remove(j);
						updateTreasureValues(gui.getTreasures().elementAt(i));
					}
				}
			}
		}
    	
    	
  
    	resetQuestionScheme();
    }
    private void resetTreasureScheme(){
    	treasureNameTextField.setText("");
    	addedQuestionsModel.removeAllElements();
    	clueTextField.setText("");
    	xCoordTextField.setText("");
    	yCoordTextField.setText("");
    	treasuresComboBox.removeAll();
    	fillCombo(gui.getTreasures());
    	selectedTreasure = null;
    	
    }
    private boolean questionCheck(String name){
    	for (int i = 0; i < selectedTreasure.getQuestions().size(); i++) {
			if(selectedTreasure.getQuestions().elementAt(i).getQuestionText().get(0).equals(name)){
				return false;
			}
		}
    	return true;
    }
    
    private void resetTreasureScheme2(){
    	treasureNameTextField.setText("");
    	addedQuestionsModel.removeAllElements();
    	clueTextField.setText("");
    	xCoordTextField.setText("");
    	yCoordTextField.setText("");
    }
    
    private void addQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	if(questionCheck(newQuestionNameTextField.getText())){
    		SessionQuestion question = new SessionQuestion();
	    	Vector<String> texts = new Vector<String>();
	    	texts.add(newQuestionNameTextField.getText());
	    	question.setQuestionText(texts);
	    	Vector<SessionOption> sessionOptions = new Vector<SessionOption>();
	    	SessionOption option1 = new SessionOption();
	    	option1.setAnswerOption(option1TextField.getText());
	    	
	    	sessionOptions.add(option1);
	    	SessionOption option2 = new SessionOption();
	    	option2.setAnswerOption(option2TextField.getText());
	    	sessionOptions.add(option2);
	    	SessionOption option3 = new SessionOption();
	    	option3.setAnswerOption(option3TextField.getText());
	    	sessionOptions.add(option3);
	    	SessionOption option4 = new SessionOption();
	    	option4.setAnswerOption(option4TextField.getText());
	    	sessionOptions.add(option4);
	    	if(getCorrectAnswerIndex() != 9){
	    		question.setCorrectAnswerIndex(getCorrectAnswerIndex());
	    		//
	    	}
	    	question.setOptions(sessionOptions);
	    	selectedTreasure.getQuestions().add(question);
	    	for (int i = 0; i < selectedTreasure.getQuestions().size(); i++) {
				System.out.println(selectedTreasure.getQuestions().elementAt(i).getQuestionText().get(0));
			}
	    	addedQuestionsModel.addElement(newQuestionNameTextField.getText());
	    	resetQuestionScheme();
    	}
    }
    private void resetQuestionScheme(){
    	newQuestionNameTextField.setText("");
    	option1CheckBox.setSelected(false);
    	option2CheckBox.setSelected(false);
    	option3CheckBox.setSelected(false);
    	option4CheckBox.setSelected(false);
    	option1TextField.setText("");
    	option2TextField.setText("");
    	option3TextField.setText("");
    	option4TextField.setText("");
    	selectedQuestion = null;
    	deleteQuestion.setEnabled(false);
    	saveQuestionButton.setEnabled(false);
    	
    }
    private void fillCombo(Vector<Treasures> treasures){
    	treasuresComboModel.removeAllElements();
    	for (int i = 0; i < treasures.size(); i++) {
    		treasuresComboModel.addElement(treasures.get(i).getName());
    	}
    }
    private int getCorrectAnswerIndex(){
    	if(option1CheckBox.isSelected()){
    		return 0;
    	}else if(option2CheckBox.isSelected()){
    		return 1;
    	}else if(option3CheckBox.isSelected()){
    		return 2;
    	}else if(option4CheckBox.isSelected()){
    		return 3;
    	}else{
    		return 9;
    	}
    }
    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt){
    	addNewQuestion.setEnabled(true);
    	deleteTreasureButton.setEnabled(true);
    	saveTreasureButton.setEnabled(true);
    	removeSelectedButton.setEnabled(true);
    	resetQuestionScheme();
		resetTreasureScheme2();
    	String treasureName = (String) treasuresComboBox.getModel().getSelectedItem();
		Treasures loadedTreasure = loadTreasure(treasureName);
		
		if(loadedTreasure != null){
			updateTreasureValues(loadedTreasure);
			selectedTreasure = loadedTreasure;
			oldTreasureName = selectedTreasure.getName();
		}else{
			System.out.println("feil ");
		}
		
    }
    private Treasures loadTreasure(String name){
    	Treasures treasure = new Treasures();
    	for (int i = 0; i < gui.getTreasures().size(); i++) {
			if(gui.getTreasures().elementAt(i).getName().equals(name)){
				treasure.setClue(gui.getTreasures().elementAt(i).getClue());
				treasure.setName(name);
				treasure.setQuestions(gui.getTreasures().elementAt(i).getQuestions());
				treasure.setXCoordinate(gui.getTreasures().elementAt(i).getXCoordinate());
				treasure.setYCoordinate(gui.getTreasures().elementAt(i).getYCoordinate());
				break;
			}
		}
    	return treasure;
    }
    private void updateTreasureValues(Treasures treasure){
    	treasureNameTextField.setText(treasure.getName());
    	xCoordTextField.setText(""+treasure.getXCoordinate());
    	yCoordTextField.setText(""+treasure.getYCoordinate());
    	clueTextField.setText(treasure.getClue().elementAt(0));
    	addedQuestionsModel.removeAllElements();
    	for (int i = 0; i < treasure.getQuestions().size(); i++) {
			addedQuestionsModel.addElement(treasure.getQuestions().elementAt(i).getQuestionText().elementAt(0));
			addedQuestions.repaint();
		}
    }
    private void selectQuestionActionPerformed(java.awt.event.ActionEvent evt){
    	String questionName = (String) addedQuestions.getSelectedValue();
    	
    	System.out.println("Utenfor IF "+questionName);
    	if(questionName != null){
    		for (int i = 0; i < selectedTreasure.getQuestions().size(); i++) {
    			System.out.println("Her Kommer QuestionName inne i For"+questionName);
    			System.out.println("!!!!!!!!!!!!!");
    			System.out.println("Selected.blalblal"+selectedTreasure.getQuestions().elementAt(i).getQuestionText().elementAt(0));
    			if(questionName.equals(selectedTreasure.getQuestions().elementAt(i).getQuestionText().elementAt(0))){
    				deleteQuestion.setEnabled(true);
    				saveQuestionButton.setEnabled(true);
    				selectedQuestion = selectedTreasure.getQuestions().elementAt(i);
    				selectedQuestionPosition = i;
    				System.out.println("Selected Question er nr #"+i);
    				newQuestionNameTextField.setText(questionName);
    				option1TextField.setText(""+selectedTreasure.getQuestions().elementAt(i).getOptions().elementAt(0).getAnswerOption());
    				option2TextField.setText(""+selectedTreasure.getQuestions().elementAt(i).getOptions().elementAt(1).getAnswerOption());
    				option3TextField.setText(""+selectedTreasure.getQuestions().elementAt(i).getOptions().elementAt(2).getAnswerOption());
    				option4TextField.setText(""+selectedTreasure.getQuestions().elementAt(i).getOptions().elementAt(3).getAnswerOption());
    				if(selectedTreasure.getQuestions().elementAt(i).getCorrectAnswerIndex()==0){
    					option1CheckBox.setSelected(true);
    				}else if (selectedTreasure.getQuestions().elementAt(i).getCorrectAnswerIndex()==1){
    					option2CheckBox.setSelected(true);
    				}else if (selectedTreasure.getQuestions().elementAt(i).getCorrectAnswerIndex()==2){
    					option3CheckBox.setSelected(true);
    				}else if (selectedTreasure.getQuestions().elementAt(i).getCorrectAnswerIndex()==3){
    					option4CheckBox.setSelected(true);
    				}
    				break;
    			}
    		}
    	}
    }

    // Variables declaration - do not modify                     
	private javax.swing.JButton addNewQuestion;
	private javax.swing.JLabel addQuestionsLabel;
	private javax.swing.JList addedQuestions;
	private javax.swing.JLabel clueLabel;
	private javax.swing.JTextField clueTextField;
	private javax.swing.JButton deleteQuestion;
	private javax.swing.JButton deleteTreasureButton;
	private javax.swing.JLabel headLabel;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JLabel newQuestionLabel;
	private javax.swing.JLabel newQuestionNameLabel;
	private javax.swing.JTextField newQuestionNameTextField;
	private javax.swing.JCheckBox option1CheckBox;
	private javax.swing.JLabel option1Label;
	private javax.swing.JTextField option1TextField;
	private javax.swing.JCheckBox option2CheckBox;
	private javax.swing.JLabel option2Label;
	private javax.swing.JTextField option2TextField;
	private javax.swing.JCheckBox option3CheckBox;
	private javax.swing.JLabel option3Label;
	private javax.swing.JTextField option3TextField;
	private javax.swing.JCheckBox option4CheckBox;
	private javax.swing.JLabel option4Label;
	private javax.swing.JTextField option4TextField;
	private javax.swing.JButton removeSelectedButton;
	private javax.swing.JButton saveQuestionButton;
	private javax.swing.JButton saveTreasureButton;
	private javax.swing.JButton selectTreasureButton;
	private javax.swing.JLabel treasureNameLabel;
	private javax.swing.JTextField treasureNameTextField;
	private javax.swing.JComboBox treasuresComboBox;
	private javax.swing.JLabel xCoordLabel;
	private javax.swing.JTextField xCoordTextField;
	private javax.swing.JLabel yCoordLabel;
	private javax.swing.JTextField yCoordTextField;
	private javax.swing.DefaultComboBoxModel treasuresComboModel;
	private javax.swing.DefaultListModel addedQuestionsModel;
	
    // End of variables declaration                   

}
